package dao

import (
	"errors"
	"fresh-e-commerce/models"
	"github.com/google/uuid"
	"go.uber.org/zap"
)

func OrderCreate(uid int64, pid int64, num int64) (int64, error) {
	p := models.Product{}
	err := p.GetProductIdInfo(pid)
	if err != nil {
		zap.Error(err)
		return 0, errors.New("商品信息查询失败")
	}
	o := models.Order{
		UserId:       uid,
		OrderSn:      uuid.New().String(),
		OrderPrice:   p.ProductPrice * float64(num),
		OrderStatus:  2,
		Payment:      1,
		ProductTitle: p.ProductTitle,
		ProductPrice: p.ProductPrice,
		ProductNum:   p.ProductNum,
	}
	err = o.OrderCreate()
	if err != nil {
		zap.Error(err)
		return 0, errors.New("订单创建失败")
	}
	return int64(o.ID), nil
}
